import datetime
import json
import time

from openpyxl import Workbook

from openpyxl import load_workbook

import pandas as pd
import common_fix as cufix
import requests
import json

# 目标URL
url = 'https://inv-web.yintaerp.com/api/inv/invWarehouseRatio/executeLogicStock'

# 请求体数据
# data = {
#     "key1": "value1",
#     "key2": "value2"
# }
"""insert into inv_pur_in_transit_info(zpo_no,sku_code,old_sku_code,platform,store,supplier,qty,origin_qty,delivery_time,create_by,update_by)
values("ZPO24092700548","FTDFGF-0014","FTDFGF-0014","10001","10415","ZJYL",339,339,"2025-01-05 00:00:00","YTWH142-fix","YTWH142-fix");"""

qco_str = """
insert into inv_pur_in_transit_info(zpo_no,sku_code,old_sku_code,platform,store,supplier,qty,origin_qty,delivery_time,create_by,update_by)
values("ZPO24092700548","FTDFGF-0014","FTDFGF-0014","10001","10415","ZJYL",339,339,"2025-01-05 00:00:00","YTWH142-fix","YTWH142-fix");
"""

flow_str = """
insert into inv_pur_flow_info(zpo_no,sku_code,old_sku_code,platform,store,supplier,biz_type,purchase_qty,oper_type,remark,create_by,update_by)
values("ZPO24092700548","FTDFGF-0014","FTDFGF-0014","10001","10415","ZJYL",'501',339,'PURCHASE_BILL_UPDATE','2025-01-02-fix','YTWH142-fix','YTWH142-fix');
"""

filename = "D:\\tmp\\mapping\\执行结果19 (1).xlsx"
tmpfile = "d:\\tmp\\result.txt"


def initlist():
    aa = list()
    return aa


# df = pd.read_excel(filename)
i = 0
# 加载现有的Excel文件
with open(tmpfile, "w") as filewriter:
    workbook = load_workbook(filename)
    po_zip_list = {}
    # 选择工作表
    sheet = workbook.active
    skulist = []
    result = []
    for row in sheet.iter_rows(min_row=2, max_row=sheet.max_row, values_only=True):
        if row[1] is None:
            break

        # skulist = load_value["skuList"]
        # for sku in skulist:
        #     sku["holdQty"] = 0
        #     sku["totalQty"] = sku["useQty"]
        # load_value = json.dumps(load_value)

        sku = row[4]
        skulist.append(sku)
        if (len(skulist) + 1) % 50 == 0:
            time.sleep(1)
            res = cufix.getsalemapping(skulist)
            if len(res) == 0:
                continue
            res = json.dumps(res)
            res = json.loads(res)
            result.extend(sku_res["sellerSku"]+"\n" for sku_res in res)
            skulist = initlist()
        # print(skulist)
        # 发送POST请求
        # response = requests.post(url, json=load_value, headers=headers)
        # print(response.text)
        # 检查响应状态码
        # if response.status_code == 200:
        #     print("请求成功")
        #     # 处理响应数据，这里以JSON为例
        #     response_data = response.json()
        #     print(response_data)
        # else:
        #     print(f"请求失败，状态码：{response.status_code}")
        i += 1
        # if i >= 20:
        #     break
    print("result  ",result)
    res_json = ""
    res_json = res_json.join(result)
    print(res_json)
    filewriter.write(res_json)
